<?php
class UserController extends Zend_Controller_Action{
	

	public function init()
	{
		
		$this->oLoggedInUser = Default_Service_Auth::getLoggedInUser();

		
	}
	public function indexAction()
	{
		   $id = $this->oLoggedInUser->getId();
		   $request = $this->getRequest();
		   if($request->isPost())
	       {
	       		$name 	  = $request->getPost('name');
	       		$fileName       = $_FILES['file_name']['name'];
			

				$destinationFilePath = APPLICATION_PATH . "/../public/files/user" ;
				$tmp_name = $_FILES["file_name"]["tmp_name"];
				
	      		 if ($fileName != "")
				{
						
						if (! file_exists ( $destinationFilePath ))
						{
							mkdir ( $destinationFilePath, 0755, TRUE ); // create the directory structure
						}
			 		
				        $path = $destinationFilePath."/".$_FILES['file_name']['name'];		   
				             	
			        		
		        	
						$oMedia = new Media();											
						$oMedia->setOwnerId($id)
						       ->setName($name)
						       ->setType('user')
						       ->setFileType('image')
							   ->setFileName($fileName)
							   ->setFileSize($_FILES['file_name']['size'])
							   ->setMimeType($_FILES['file_name']['type'])
							   ->setAddedDate(date('Y-m-d'))
							   ->save();   	
					 move_uploaded_file($tmp_name,$path);
					 $this->_helper->redirector("index", "index","account");				   
			    }
	       }
	}
	public function editAction()
	{
		   $id = $this->oLoggedInUser->getId();
		   
		   $criteria = new Criteria();
		   $criteria->add(MediaPeer::OWNER_ID,$id,Criteria::EQUAL);
		   $criteria->add(MediaPeer::TYPE,'user',Criteria::EQUAL);
		   $userRes = MediaPeer::doSelectOne($criteria);
		   
		   $name = $userRes->getName();
		   $mediaId = $userRes->getId();
		   $fileName = $userRes->getFileName();
		   
	
		   $id = $this->oLoggedInUser->getId();
		   $request = $this->getRequest();
		   if($request->isPost())
	       {
	       		$name 	  = $request->getPost('name');
	       		$mediaId  = $request->getPost('mediaId');
	       		$mediaFileName = $request->getPost('mediaFile');
	       		$fileName       = $_FILES['file_name']['name'];
			
				
				$destinationFilePath = APPLICATION_PATH . "/../public/files/user" ;
				$tmp_name = $_FILES["file_name"]["tmp_name"];
				
	      		 if ($fileName != "")
				{
				
						
						if (! file_exists ( $destinationFilePath ))
						{
							mkdir ( $destinationFilePath, 0755, TRUE ); // create the directory structure
						}
			 		
				        $path = $destinationFilePath."/".$_FILES['file_name']['name'];		   
				             	
			        		
		        	
						$oMedia = MediaPeer::retrieveByPK($mediaId);										
						$oMedia->setOwnerId($id)
						       ->setName($name)
						       ->setType('user')
						       ->setFileType('image')
							   ->setFileName($fileName)
							   ->setFileSize($_FILES['file_name']['size'])
							   ->setMimeType($_FILES['file_name']['type'])
							   ->setAddedDate(date('Y-m-d'))
							   ->save();   	
							   
					 move_uploaded_file($tmp_name,$path);
					 unlink(APPLICATION_PATH . "/../public/files/user/" . $mediaFileName);
					 $this->_helper->redirector("index", "index","account");				   
			    }
	       }
		   
		   
		   $this->view->name = $name;
		   $this->view->mediaId = $mediaId;
		   $this->view->fileName = $fileName;
		   
		   
		   
		   
	}
	public function updateAction()
	{
	
	   echo "hllo";
		   $id = $this->oLoggedInUser->getId();
		   $request = $this->getRequest();
		   if($request->isPost())
	       {
	       		$name 	  = $request->getPost('name');
	       		$mediaId  = $request->getPost('mediaId');
	       		$mediaFileName = $request->getPost('mediaFile');
	       		$fileName       = $_FILES['file_name']['name'];
			
				
				$destinationFilePath = APPLICATION_PATH . "/../public/files/user" ;
				$tmp_name = $_FILES["file_name"]["tmp_name"];
				
	      		 if ($fileName != "")
				{
				echo "update";
						
						if (! file_exists ( $destinationFilePath ))
						{
							mkdir ( $destinationFilePath, 0755, TRUE ); // create the directory structure
						}
			 		
				        $path = $destinationFilePath."/".$_FILES['file_name']['name'];		   
				             	
			        		
		        	
						$oMedia = MediaPeer::retrieveByPK($mediaId);										
						$oMedia->setOwnerId($id)
						       ->setName($name)
						       ->setType('user')
						       ->setFileType('image')
							   ->setFileName($fileName)
							   ->setFileSize($_FILES['file_name']['size'])
							   ->setMimeType($_FILES['file_name']['type'])
							   ->setAddedDate(date('Y-m-d'))
							   ->save();   	
							   
					 move_uploaded_file($tmp_name,$path);
					 unlink(APPLICATION_PATH . "/../public/files/user/" . $mediaFileName);
					 //$this->_helper->redirector("index", "index","account");				   
			    }
	       }
	}
	public function changepwdAction()
	{
		$id = $this->oLoggedInUser->getId();
			
			$criteria = new Criteria();
			$criteria->add(UserPeer::ID,$id,Criteria::EQUAL);
			$userRes = UserPeer::doSelectOne($criteria);
			
			$userpwd = $userRes->getPassword();
			
			
		   $request = $this->getRequest();
		   if($request->isPost())
	       {
	       			$oldPwd 	  = md5($request->getPost('old_pwd'));
	       			$newPwd       = $request->getPost('new_pwd');
	       			
	       			if($oldPwd == $userpwd)
	       			{
		       			$oUser = UserPeer::retrieveByPK($id);
		       			$oUser->setPassword(md5($newPwd))
	       				  	  ->save();	
	       				$this->view->message = "Your password has been changed.";	  
	       			}
	       			else 
	       			{
	       				$this->view->message = "Your current password is incorrect";
	       			}
	       		
	       }
	}
}	